<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateQuestionTagRelationTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        /**
         * 题目与标签是多对多关系 方便用于根据特定类型组题也可以根据特定几种标签组题
         * 例如建立标签真题可以创建多个带有真题的题目 然后使用随机组卷功能组成试卷
         *
         */
        Schema::create('question_tag_relation', function (Blueprint $table) {
            //题目标签中间表
            $table->unsignedBigInteger('tag_id')->comment('标签id');
            $table->unsignedBigInteger('question_id')->unique(true)->comment('问题id');
            //唯一索引
            $table->unique(['tag_id','question_id']);
            $table->foreign('tag_id')->references('id')->on('question_tags');
            $table->foreign('question_id')->references('id')->on('questions');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('questions');
    }
}
